package com.example.blogsystem.datacenter;

import com.example.blogsystem.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class RedisDataCenter {
    @Autowired
    private StringRedisTemplate redisTemplate;

    public void setCaptcha(String key, String value) throws Exception {
        if(!StringUtils.hasLength(key) || !StringUtils.hasLength(value)){
            log.error("[RedisDataCenter] 验证码设置失败！key: " + key + " value: " + value);
        }
        redisTemplate.opsForValue().setIfPresent(key, value, Constants.EMAIL_VALID_TIME, TimeUnit.MILLISECONDS);
    }

    public String getCaptcha(String key){
        if(!StringUtils.hasLength(key)){
            log.error("[RedisDataCenter] 验证码设置失败！key: " + key);
        }
        return redisTemplate.opsForValue().get(key);
    }
}
